package cn.edu.xaut.mybatisers.mall.Controller;

import cn.edu.xaut.mybatisers.mall.Entity.*;
import cn.edu.xaut.mybatisers.mall.ServiceImp.ShoppingTrolleyServiceImp;
import cn.edu.xaut.mybatisers.mall.tool.AReturnStructFactory;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.*;

@Controller
@RequestMapping(value = "/shoppingTrolley")
public class ShoppingTrolleyController {

    @Autowired
    ShoppingTrolleyServiceImp shoppingTrolleyService;
    /**
     * 1. 增加传[商品id与商品数量]
     * 2. 删除传[商品id与商品数量(numbers=0)]
     * 注：为0才会删除
     * **/
    @ApiOperation("添加删除商品改变商品数量")
    @RequestMapping(value = "/addCommodityInto",method = RequestMethod.POST)
    @ResponseBody
    public Object addCommodity(@RequestBody ShoppingTrolley st, HttpSession session ) throws SQLException {
        int consumer_id = (Integer)session.getAttribute("consumer_id");
        st.setConsummer_id(consumer_id);
        shoppingTrolleyService.addCommodity(st);
        return AReturnStructFactory.returnDataStructFactory(true,
                 st.getNumber() > 0 ? "添加成功" : "删除成功",
                200, null);      //跳转至页面
    }

//    @ApiOperation("商品数量加一")
//    @RequestMapping(value = "/addOne",method = RequestMethod.POST)
//    @ResponseBody
//    public Object addOne(@RequestBody ShoppingTrolley st, HttpSession session){
//        /* 传commodity_id */
//
////        ShoppingTrolley st = new ShoppingTrolley();
//        int consumer_id = (Integer)session.getAttribute("consumer_id");
//        st.setConsummer_id(consumer_id);
////        String cid1 = request.getParameter("commodity_id");
////        int cid = Integer.parseInt(cid1);
////        st.setCommodity_id(cid);
//        shoppingTrolleyService.addOne(st);
//         /*
//        甩锅Service层
//        * */
////        String info = "添加成功";
////        model.addAttribute("result",info);
//        return AReturnStructFactory.returnDataStructFactory(true, "添加成功", 200, null);      //跳转至页面
//    }
//
//    @ApiOperation("商品数量减一")
//    @RequestMapping(value = "/subOne",method = RequestMethod.POST)
//    @ResponseBody
//    public Object subOne(@RequestBody ShoppingTrolley st, HttpSession session){
//        /* 传commodity_id */
//
////        ShoppingTrolley st = new ShoppingTrolley();
//        int consumer_id = (Integer)session.getAttribute("consumer_id");
//        st.setConsummer_id(consumer_id);
////        String cid1 = request.getParameter("commodity_id");
////        int cid = Integer.parseInt(cid1);
////        st.setCommodity_id(cid);
//        if(shoppingTrolleyService.checkNumber(st)==1)
//            shoppingTrolleyService.deleteCommodity(st);
//        else
//            shoppingTrolleyService.subOne(st);
//         /*
//        甩锅Service层
//        * */
////        String info = "减去成功";
////        model.addAttribute("result",info);
//        return AReturnStructFactory.returnDataStructFactory(true, "减去成功", 200, null);
//    }
//    @ApiOperation("删除商品")
//    @RequestMapping(value = "/deleteCommodity",method = RequestMethod.POST)
//    @ResponseBody
//    public Object deleteCommodity(@RequestBody ShoppingTrolley st, HttpSession session){
//        /*
//         * 删除商品*/
////        ShoppingTrolley st = new ShoppingTrolley();
//        int consumer_id = (Integer)session.getAttribute("consumer_id");
//        st.setConsummer_id(consumer_id);
//
////        String cid1 = request.getParameter("commodity_id");
////        int cid = Integer.parseInt(cid1);
////        st.setCommodity_id(cid);
//        shoppingTrolleyService.deleteCommodity(st);
//        /*
//        甩锅Service层
//        * */
////        String info = "删除成功";
////        model.addAttribute("result",info);
//        return AReturnStructFactory.returnDataStructFactory(true, "删除成功", 200, null);
//    }

//    @ApiOperation("清空购物车")
//    @RequestMapping(value = "/emptyCart",method = RequestMethod.GET)
//    @ResponseBody
//    public Object emptyCart(HttpSession session){
//        int consumer_id = (Integer)session.getAttribute("consumer_id");
//
//        shoppingTrolleyService.emptyCart(consumer_id);
//        /*
//        甩锅Service层
//        * */
////        String info = "清除成功";
////        model.addAttribute("result",info);
//        return AReturnStructFactory.returnDataStructFactory(true, "清除成功", 200, null);
//    }

    @ApiOperation("结算")
    @RequestMapping(value = "/countPrice", method = RequestMethod.POST)
    @ResponseBody
    public Object countPrice(@RequestBody JSONObject jsonObject, HttpSession session){
        String orderNumber = jsonObject.getString("orderNumber");
        List<Integer> commodity_ids = (List<Integer>) JSONArray.toCollection(jsonObject.getJSONArray("commodity_id"), ArrayList.class);
         Map<String, Object> data = shoppingTrolleyService.countPrice(orderNumber, commodity_ids, session);
         return AReturnStructFactory.returnDataStructFactory(true, "结算成功", 200, data);
    }
    @ApiOperation("查询购物车商品")
    @RequestMapping(value = "/findCommodityInTrolley", method = RequestMethod.GET)
    @ResponseBody
    public Object findCommodity(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize,
            HttpSession session){
        List<HashMap<String, Object>> data = shoppingTrolleyService.findCommodity(pageNum, pageSize, session);
        return AReturnStructFactory.returnDataStructFactory(true, "查询成功", 200, data);
    }

}
